#!/usr/bin/env python
#-*- coding:utf-8 -*-
import pygtk
pygtk.require('2.0')
import gtk
from treeview import *
from core import Core
from parseFirst import *
from parseSecond import *
import thread
import threading
from statusbar import *
class window:
    def __init__(self):
        builder=gtk.Builder()
        builder.add_from_file("songtaste.glade")
        
        
        self.window=builder.get_object("window")
        self.entry=builder.get_object("entry1")
        self.button1=builder.get_object("button1")
        self.button2=builder.get_object("button2")
        self.checkbutton=builder.get_object("checkbutton")
        
        self.button1.connect("clicked",self.update_button)
        self.button2.connect("clicked",self.download)
        self.checkbutton.connect("toggled",self.select_all)
        
        self.window.set_position(gtk.WIN_POS_CENTER)
        self.window.connect("destroy",lambda q:gtk.main_quit())
        vbox=builder.get_object("vpaned1")
        self.treeview=View()
        self.model=self.treeview.get_model()
#        self.model.append(None,[1,"just try you my fail but if you give up there will no chance",False])
        self.view=DisplayModel()
        self.view1=self.view.make_view(self.model)
        self.scroll=gtk.ScrolledWindow(gtk.Adjustment())
        
        self.scroll.set_size_request(780,550)
        self.scroll.add(self.view1)
        self.status=statusbar
        vb=gtk.VBox(False,0)
        vbox.add(vb)
        vb.pack_start(self.scroll,False,False,5)
        vb.pack_start(self.status,False,False)
        self.window.add(vbox)
        
        self.window.show_all()
    def main(self):
        gtk.main()
    def update_button(self,widget):
        thread.start_new_thread(self.update, ())
    def update(self):
        self.p=parse(self.entry.get_text())
        self.songlist=self.p.getSongName()
        netlist=self.p.getSongNet()
        Core.set_song_and_net(self.songlist, netlist)
        self.treeview.update(self.songlist)
    def song_and_net(self):
        return (self.songlist,self.netlist)
    def select_all(self,widget):
        active =widget.get_active()
        if active:
            self.view.select_all()
        else:
            self.view.remove_all()
    def download(self,widget):
        thread.start_new_thread(Core.downall,(self.treeview.up_prs,))
if __name__=="__main__":
        gobject.threads_init()
        win=window()
        win.main()
    